/**
 * InsCourseStructureService.java
 * com.sftz.modules.minapp.coursestudy.service
 * Copyright (c) 2018, 北京十方天资科技有限公司版权所有.
*/

package com.sftz.modules.minapp.coursestudy.service;

import java.util.List;

import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.google.common.base.Objects;
import com.sftz.assets.enums.ResourcesSortEnum;
import com.sftz.framework.service.aliyun.AliyunVodUtil;
import com.sftz.framework.service.qiniu.QiniuDownloadService;
import com.sftz.framework.util.StringUtil;
import com.sftz.modules.minapp.coursestudy.dto.InsCourseStructureDto;
import com.sftz.modules.minapp.coursestudy.form.WebOnclickForm;
import com.uxuexi.core.common.util.EnumUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * 获取课程资源相关
 * @author   gyp
 * @Date	 2018年12月17日 	 
 */
@IocBean
public class InsCourseStructureService {

	private static final Log log = Logs.get();

	@Inject
	protected IDbDao dbDao;
	@Inject
	protected SqlManager sqlManager;
	@Inject
	private QiniuDownloadService qiniuDownloadService;

	public InsCourseStructureDto onclickView(final WebOnclickForm sqlParamForm) throws Exception {

		Sql sql = Sqls.create(sqlManager.get("minapp_courseStudy_findResource"));
		sql.params().set("structureId", sqlParamForm.getRid());
		InsCourseStructureDto resource = DbSqlUtil.fetchEntity(dbDao, InsCourseStructureDto.class, sql);
		//存在课程资源
		if (!Util.isEmpty(resource)) {
			String play_path = null;
			ResourcesSortEnum ct = EnumUtil.get(ResourcesSortEnum.class, resource.getResourceType());
			switch (ct) {
			case VIDEO://处理视频相关
				//获取视频连接的地址
				GetPlayInfoResponse response = AliyunVodUtil.getVideoInfos(resource.getResourceUrl());
				List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
				// 播放地址
				for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
					if (Objects.equal("mp4", StringUtil.getSuffix(playInfo.getPlayURL()))) {
						play_path = playInfo.getPlayURL();
						break;
					}
				}
				break;
			case AUDIO:
				//音频相关
				String playPath = "http://file.sktcm.cn/" + resource.getResourceUrl();
				play_path = qiniuDownloadService.getDownloadUrl(playPath);
				break;
			case FILE: //文件
				play_path = resource.getResourceUrl();
				break;
			case MIND: //导图
				play_path = resource.getResourceUrl();
				break;
			case LINK: //链接
				play_path = resource.getResourceUrl();
				break;
			case IMAGE_TEXT: //图文
				play_path = resource.getResourceUrl();
				break;
			default:
				break;
			}
			resource.setAddress(play_path);
		} else {
			//资源不存在
			log.error("资源不存在");
		}
		return resource;
	}

	public static void main(String[] args) {

		GetPlayInfoResponse response = AliyunVodUtil.getVideoInfos("1519034266fe41a9964228a6a3632db4");
		List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
		// 播放地址
		for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
			if (Objects.equal("mp4", StringUtil.getSuffix(playInfo.getPlayURL()))) {
				String play_path = playInfo.getPlayURL();
				System.out.println(play_path);
			}
		}
	}
}
